package com.jeeplus.modules.guide.service;


import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.RedisUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.time.DateUtil;
import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.guide.mapper.SWMapper;
import com.jeeplus.modules.guide.model.sw.HourValue;
import com.jeeplus.modules.guide.model.sw.WinValue;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * ClassName: SWService
 * Function:  税务首页统计分析
 * Date:      2022/7/4 10:53
 *
 * @author hanKongbin
 * version    V1.0
 */
@DS("queuing")
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class SWService {
    private final RedisUtils redisUtils;
    private final SWMapper swMapper;
    //高新税务总办件量redis key 统计当天，本周，本月，本年数据
    @Value("${gxswTotal}")
    private String gxswTotal;
    //高新税务总办件量redis key  统计群众当天取号记录
    @Value("${gxswPersonTotal}")
    private String gxswPersonTotal;
    //高新税务小时趋势开始时间
    @Value("${gxswHourStart}")
    private Integer gxswHourStart;
    //高新税务小时趋势结束时间
    @Value("${gxswHourEnd}")
    private Integer gxswHourEnd;



    /*
     isRun 是否加载数据进redis
    */
    public Object toTotalSW(Boolean isRun){
        HashMap<String,Integer> map = new HashMap<String,Integer>();
        if(redisUtils.hasKey(gxswTotal) && !isRun){
            return redisUtils.get(gxswTotal);
        } else{
            map.put("dayNum",swMapper.toTatalToDay());
            map.put("weekNum",swMapper.toTatalWeek());
            map.put("monthNum",swMapper.toTatalMonth());
            map.put("allNum",swMapper.toTatalYear()+swMapper.toTatalYearHistory());
            redisUtils.set(gxswTotal, map);
            return map;
        }
    }

    public AjaxJson toHourSW(){
        List<HourValue> hourValues = swMapper.toHourSW(gxswHourStart,gxswHourEnd);
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        int len = gxswHourEnd-gxswHourStart+1;
        for(int i=0;i<len;i++){
            if((i+1) > hourValues.size()){
                HourValue hv = new HourValue();
                hv.setNumber(0);
                hv.setDoTime((gxswHourStart+i)+":00");
                hourValues.add(hv);

            }
            nameList.add(hourValues.get(i).getDoTime());
            valueList.add(hourValues.get(i).getNumber());
        }
        hourValues.clear();
        SimpleDateFormat formatter= new SimpleDateFormat("HH");
        Date date = new Date(System.currentTimeMillis());
        String format = formatter.format(date);
        int i = Integer.parseInt(format);
        for (int k = (i-10); k < valueList.size(); k++) {
            valueList.remove(k);
        }
        return AjaxJson.success().put("nameList",nameList).put("valueList",valueList);
    }

    public List<HashMap<String,Object>> toWinTopSW(){
        List<HashMap<String, Object>> hashMaps = swMapper.toWinTopSW();
        for(HashMap<String, Object> m : hashMaps){
            if("5101090000004".equals(m.get("areaId"))){
                m.put("winNum",m.get("winNum")+"（A区）");
            }else if("5101090000005".equals(m.get("areaId"))){
                m.put("winNum",m.get("winNum")+"（B区）");
            }else{
                m.put("winNum",m.get("winNum")+"（C区）");
            }
        }
        return swMapper.toWinTopSW();
    }

    public List<HashMap<String,Object>> toDayTypeSW(){
        List<HashMap<String, Object>> hashMaps = swMapper.toDayTypeSW();
        for(HashMap<String, Object> m : hashMaps){
            if(null == m.get("doNum")){
                m.put("doNum",0);
            }
            if(null == m.get("waitNum")){
                m.put("waitNum",0);
            }
        }
        return hashMaps;
    }

    public AjaxJson toAllTypeSW(){
        List<HashMap<String, Object>> map = swMapper.toAllTypeSW();
        List <HashMap <String, Object>> list = swMapper.toAllTypeSWHistory();
        for(HashMap <String, Object> m : list){
            for(HashMap <String, Object> ms : map){
                if(m.get("name").equals(ms.get("name"))){
                    ms.put("value",Integer.valueOf(ms.get("value").toString())+Integer.valueOf(m.get("num").toString()));
                }
            }
        }
        int allTotal = 0;
        for(int i=0;i<map.size();i++){
            map.get(i).put("areaId",i);
            allTotal += Integer.valueOf(map.get(i).get("value").toString());
        }
        return AjaxJson.success().put("data",map).put("allTotal",allTotal);
    }

    @Transactional(readOnly = false)
    public int intoWaitPerson(String comeTime,int waitNum,String tell,String areaId,String number,int type){
        return swMapper.intoWaitPerson(comeTime,waitNum,tell,areaId,number,type);
    }

    /*
     isRun 是否加载数据进redis
    */
    public Object toWaitPersonList(Boolean isRun){
        if(redisUtils.hasKey(gxswPersonTotal) && !isRun){
            return redisUtils.get(gxswPersonTotal);
        } else{
            List<HashMap<String, Object>> map = swMapper.toWaitPersonList();
            for(HashMap<String, Object> m : map){
                m.put("name",m.get("name").toString().substring(0,1)+"**");
                if(null == m.get("waitNum"))m.put("waitNum",0);
            }
            redisUtils.set(gxswPersonTotal, map);
            return map;
        }
    }



    @Transactional(readOnly = false)
    public void toScheduldelUserHistory(){
        swMapper.toScheduldelUserHistory();
    }


}